﻿angular.module('resources.customerProxy', []);
angular.module('resources.customerProxy').factory('customerProxy', ['rresource', '$http', 'APPSETTING', function (rresource, $http, APPSETTING) {
    var factory = rresource('customer');

    factory.searchcustomer = function (filterStr, successcb, failedcb) {

        return factory.odataQuery(filterStr, successcb, failedcb);

    };

    factory.getByNum = function (num, successcb, failedcb) {
        return factory.queryObject({'num': num}, successcb, failedcb);
    }

    factory.customerPaging = function (index, itemCount, filter, successcb, failedcb) {
// var filter ="&$filter=(RemoveFlg eq'" + "1" + "')"+filter;
//        if (filter != "") {
//            filter += "and (RemoveFlg eq'" + "1" + "')";
//        } else {
//            filter += "&$filter=(RemoveFlg eq'" + "1" + "')"; 
//         }
        var itemspage = (index - 1) * itemCount;
        var filterStr = "$top=" + itemCount + "&$skip=" + itemspage + "&$orderby= CustomerNum asc " + filter + "&$count=true";
        return factory.odataQuery(filterStr, successcb, failedcb);
    };

    factory.exportByCondition = function (num, name, status, group, collector, isic, begintime, endtime,
                                               miscollector, misgroup,billcode,country, successcb, failedcb) {
        $http({
            url: APPSETTING['serverUrl'] + '/api/customer?custnum=' + num + '&custname=' + name+'&status='+status+
                                '&group='+group+'&collector='+collector+'&isic='+isic+'&begintime='+begintime+
                                '&endtime=' + endtime + '&miscollector=' + miscollector + '&misgroup=' + misgroup +
                                '&billcode=' + billcode + '&country=' + country,
            method: 'POST'
        }).success(function (response) {
            successcb(response);
        }).error(function (ex) {
            failedcb(ex);
        });
    };

    factory.finishSOA = function (filter, successcb, failedcb) {

        $http({
            url: APPSETTING['serverUrl'] + '/api/customer',
            method: 'POST',
            data: filter
        }).success(function (response) {
            successcb(response);
        }).error(function (ex) {
            failedcb(ex);
        });
    };

    return factory;
} ]);

